/*
 * 百度在线网络技术（北京）有限公司拥有本软件版权2020并保留所有权利。
 * Copyright 2020, Baidu.com,Inc 2:Baidu Online Network Technology (Beijing) Co.,Ltd,
 * All rights reserved.
 */

package com.azdebugit.threapool.executor_submit;

import com.google.common.collect.Lists;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

public class ExecutorAndSubmit {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        final ExecutorService pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors());
//        List<Integer> list = Lists.newArrayList(1, 2, 3, 5, 6, null);
        List<Integer> list = Lists.newArrayList(1, 2, 3, 5, 6);
        //1.使用submit
        long startTime=System.nanoTime();   //获取开始时间
        pool.submit(() -> {
            list.parallelStream().map(a -> a.toString()).collect(Collectors.toList());
        });
        long endTime=System.nanoTime(); //获取结束时间
        System.out.println("程序运行时间submit： "+(endTime-startTime)+"ns");
        TimeUnit.SECONDS.sleep(3);
        //2.使用 execute
        long startTime1=System.nanoTime();   //获取开始时间
        pool.execute(() -> {
            list.parallelStream().map(a -> a.toString()).collect(Collectors.toList());
        });
        long endTime1=System.nanoTime(); //获取结束时间
        System.out.println("程序运行时间execute： "+(endTime1-startTime1)+"ns");
        //3.使用submit，调用get()
        long startTime2=System.nanoTime();   //获取开始时间
        pool.submit(() -> {
            list.parallelStream().map(a -> a.toString()).collect(Collectors.toList());
        }).get();
        long endTime2=System.nanoTime(); //获取结束时间
        System.out.println("程序运行时间submit get： "+(endTime2-startTime2)+"ns");
        TimeUnit.SECONDS.sleep(3);
    }
}
